<UserControl x:Class="ArchiSimple.Windows.View.PlanView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:i="using:System.Windows.Interactivity"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:vm="using:ArchiSimple.Windows.ViewModel"        
             xmlns:mvvm="using:GalaSoft.MvvmLight.Command"
             xmlns:locale="using:ArchiSimple.Windows"
             xmlns:v="using:ArchiSimple.Windows.View"
             locale:Locator.Locate="vm:PlanViewModel"   
             d:DataContext="{d:DesignInstance vm:PlanViewModel, IsDesignTimeCreatable=True}"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <locale:BooleanToVisibilityConverter x:Key="locale:BooleanToVisibilityConverter"/>
    </UserControl.Resources>
    <Grid Margin="0,-1,0,1">
        <Grid.Resources>
            <locale:PlanObjectDataTemplateSelector x:Key="PlanObjectDataTemplateSelector" />
            <locale:PlanObjectDataTemplateSelector x:Key="PlanObjectDataTemplateSelectorAbsolute" Suffix="Absolute" />
            <locale:PlanObjectStyleSelector x:Key="PlanObjectStyleSelector" />
            <Style x:Key="RoomStyle" TargetType="ListBoxItem">
                <Setter Property="Canvas.ZIndex" Value="10" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualState x:Name="Unselected"/>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="RoomSelect" Storyboard.TargetProperty="Opacity"  To="0.5" />
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Path x:Name="RoomSelect" Data="{Binding SelectedGeometry}" StrokeThickness="10" Fill="{StaticResource BlueSolidColor}" Opacity="0" Stroke="Transparent" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="WallStyle" TargetType="ListBoxItem">
                <Setter Property="Canvas.ZIndex" Value="20" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualState x:Name="Unselected"/>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="WallSelect" Storyboard.TargetProperty="Opacity"  To="0.5" />
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Path x:Name="WallSelect" Data="{Binding SelectedGeometry}" StrokeThickness="10" Opacity="0" Stroke="{StaticResource BlueSolidColor}"  />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="PointStyle" TargetType="ListBoxItem">
                <Setter Property="Canvas.Left" Value="{Binding X}" />
                <Setter Property="Canvas.Top" Value="{Binding Y}" />
                <Setter Property="Canvas.ZIndex" Value="30" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualState x:Name="Unselected"/>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="PointSelect" Storyboard.TargetProperty="Opacity"  To="0.5" />
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle x:Name="PointSelect" Opacity="0" Fill="{StaticResource BlueSolidColor}" Width="20" Height="20" Margin="-10,-10,0,0" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <DataTemplate x:Key="Room">
                <Grid Width="{Binding Zone.Width}" Height="{Binding Zone.Height}">
                    <TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </DataTemplate>
            <DataTemplate x:Key="RoomAbsolute">
                <Grid>
                    <Path Data="{Binding ZoneGeometry}" StrokeThickness="0">
                        <Path.Fill>
                            <SolidColorBrush Color="{Binding Color}" />
                        </Path.Fill>
                    </Path>
                </Grid>
            </DataTemplate>
            <DataTemplate x:Key="Wall">
            </DataTemplate>
            <DataTemplate x:Key="WallAbsolute">
            </DataTemplate>
            <DataTemplate x:Key="Point">
            </DataTemplate>
            <DataTemplate x:Key="PointAbsolute">
            </DataTemplate>
        </Grid.Resources>
        <Grid.Background>
            <ImageBrush ImageSource="ArchiSimple.Metro;component/Content/Grid.png" Stretch="None"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <Button Content="Mur" Command="{Binding AddWallCommand}" Foreground="{StaticResource LightGraySolidColor}" FontSize="36">
                <Button.Template>
                    <ControlTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" />
                                <ColumnDefinition Width="300" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Stroke="{StaticResource LightGraySolidColor}" Width="32" Height="32" Margin="4" VerticalAlignment="Center" HorizontalAlignment="Center">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="ArchiSimple.Metro;component/Content/Wall.png"  AlignmentX="Left" AlignmentY="Top"  />
                                </Rectangle.Fill>
                            </Rectangle>
                            
                            <TextBlock LineHeight="36" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Mur" LineStackingStrategy="BlockLineHeight" />
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </StackPanel>
        <Canvas Grid.Row="1" >
            <Canvas.Background>
                <ImageBrush AlignmentX="Left" AlignmentY="Top" ImageSource="ArchiSimple.Metro;component/Content/Grid.png" Stretch="None"   />
            </Canvas.Background>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PointerPressed">
                    <mvvm:EventToCommand Command="{Binding DownPlanCommand}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="PointerReleased">
                    <mvvm:EventToCommand Command="{Binding UpPlanCommand}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="PointerMove">
                    <mvvm:EventToCommand Command="{Binding PreviewWallCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <i:Interaction.Behaviors>
                <locale:PositionBehavior MousePoint="{Binding MousePoint, Mode=TwoWay}" />
            </i:Interaction.Behaviors>

            <ItemsControl x:Name="PlanObjectAbsolute" ItemsSource="{Binding PlanObjects}" ItemTemplateSelector="{StaticResource PlanObjectDataTemplateSelectorAbsolute}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

            <ItemsControl x:Name="PlanObjectRelative" ItemsSource="{Binding PlanObjects}" ItemTemplateSelector="{StaticResource PlanObjectDataTemplateSelector}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <locale:PlanObjectPanel />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

            <Path x:Name="WallBorder" Data="{Binding WallGeometry}" StrokeThickness="12" Stroke="{StaticResource LightGraySolidColor}" StrokeStartLineCap="Square" StrokeEndLineCap="Square" />
            <Path x:Name="Wall" Data="{Binding WallGeometry}" StrokeThickness="10" StrokeStartLineCap="Square" StrokeEndLineCap="Square" >
                <Path.Stroke>
                    <ImageBrush ImageSource="ArchiSimple.Metro;component/Content/Wall.png" Stretch="None"  AlignmentX="Left" AlignmentY="Top"  />
                </Path.Stroke>
            </Path>

            <ListBox ItemsSource="{Binding PlanObjects}" Margin="0,0,0,0" ItemContainerStyleSelector="{StaticResource PlanObjectStyleSelector}" SelectedItem="{Binding CurrentPlanObject, Mode=TwoWay}" Visibility="{Binding Mode, ConverterParameter=SelectPlanObject, Converter={StaticResource EnumToVisibility}}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="PointerPressed">
                        <mvvm:EventToCommand Command="{Binding DownPlanCommand}" />
                    </i:EventTrigger>
                    <i:EventTrigger EventName="PointerReleased">
                        <mvvm:EventToCommand Command="{Binding UpPlanCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Grid />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.Template>
                    <ControlTemplate>
                        <Grid>
                            <ItemsPresenter />
                        </Grid>
                    </ControlTemplate>
                </ListBox.Template>
            </ListBox>
            <Canvas x:Name="Cursor" Height="17" Canvas.Left="{Binding MouseX}" Canvas.Top="{Binding MouseY}" Width="17" Visibility="{Binding Mode, ConverterParameter=AddWall, Converter={StaticResource EnumToVisibility}}" Margin="-8,-8,0,0">
                <Rectangle Fill="{StaticResource GraySolidColor}" Width="4" Height="1" Canvas.Left="13" Canvas.Top="8" />
                <Rectangle Fill="{StaticResource GraySolidColor}" Width="4" Height="1" Canvas.Left="0" Canvas.Top="8" />
                <Rectangle Fill="{StaticResource GraySolidColor}" Width="1" Height="4" Canvas.Left="8" Canvas.Top="0" />
                <Rectangle Fill="{StaticResource GraySolidColor}" Width="1" Height="4" Canvas.Left="8" Canvas.Top="13" />
                <Ellipse  Width="5" Height="5" Fill="{StaticResource BlueSolidColor}" Canvas.Left="6" Canvas.Top="6" />
            </Canvas>
        </Canvas>
        <Rectangle Width="5" HorizontalAlignment="Left" Grid.Row="1" >
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="1,0" StartPoint="0,0" Opacity="0.5">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Height="5" VerticalAlignment="Top" Grid.Row="1" >
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0" Opacity="0.5">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</UserControl>
